<?php 

/**
 * 商品管理控制器
 */
class ShopController extends AuthController{
	
	private $model;
	//构造函数
	public function __auto(){
		$this->model = K('Shop');
	}
	
	public function index(){
//		分页
		$total = $this->model->count();
		//分页类的使用
		$pageobj = new Page($total,5,3);
		//显示分页
		$page = $pageobj->show();
		$this->assign('page',$page);
		$data = $this->model->limit($pageobj->limit())->all();
//		p($data);die;
		$this->assign('data',$data);
		
		
		
		$this->display();
		
		
		
		
	}
	
//	商品添加 
	public function add(){
		if(IS_POST){
			if(!$gid = $this->model->addData()){
				$this->error($this->model->error);
			}
			S('selected_'.$gid,$_POST['selected']);
			$this->success('添加成功',U('index'));
		}
		//处理分类数据
		$cateData = K('Cate')->all();
		$cateData = Data::tree($cateData,'cname');
		$this->assign('cateData',$cateData);
//		p($cateData);
		//处理品牌数据
		$brandData = K('Brand')->all();
		$this->assign('brandData',$brandData);
		
		
		$this->display();
	}
	
	/**
	 * 通过cid获得类型属性
	 */
	public function getTypeAttr(){
		if(!IS_AJAX) return;
		$cid = Q('post.cid',0,'intval');

		$data = M()->join('__type_attr__ ta JOIN __category__ c ON ta.type_idtype=c.type_idtype')->where("cid={$cid}")->all();
		//把类型属性的值按照,拆分
		
		foreach ($data as $k => $v) {
			$data[$k]['type_value'] = explode(',', $v['type_value']);
		}
		echo json_encode($data);
		exit;
	}
	/**
	 * 列表图上传
	 */
	public function uploadList(){
		$upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
	/*
	 * 商品图册上传
	 */
	public function uploadPic(){
		$upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {//上传成功
    		$type = strrchr($file[0]['path'], '.');
    		//缩略操作
    		$img = new Image();
			
			//缩略小图
			$smallPath = str_replace($type, '_small' . $type, $file[0]['path']);
			$small = $img->thumb($file[0]['path'],$smallPath,200,200,2);
			//缩略中图
			$midPath = str_replace($type, '_mid' . $type, $file[0]['path']);
			$mid = $img->thumb($file[0]['path'],$midPath,400,400,2);
			//缩略大图
			$bigPath = str_replace($type, '_big' . $type, $file[0]['path']);
			$big = $img->thumb($file[0]['path'],$bigPath,800,800,2);
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
		/**
	 * 删除商品
	 */
	 public function del(){
	 	$gid = Q('get.gid',0,'intval');
		//删除商品表中的图片（必须是先删除图片，因为需要表中的数据）
		$img = $this->model->where("gid={$gid}")->find();
		if(is_file($img['pic'])) unlink($img['pic']);
		//删除商品表中的数据
		$this->model->where("gid={$gid}")->delete();
		//删除商品属性表中的数据
		K('Goods_Attr')->where("goods_gid={$gid}")->delete();
		
		//删除商品详细表中的数据
		$detail=K('Detail')->where("goods_gid={$gid}")->find();
		$detail=unserialize($detail['shop_pic']);
		foreach ($detail as $k => $v) {
			//获得类型
			$typpe=strrchr($v, '.');
			//小图
			$smallPath=str_replace($typpe, '_small'.$typpe,$v);
			//中图
			$midPath=str_replace($typpe, '_mid'.$typpe,$v);
			//大图
			$bigPath=str_replace($typpe, '_big'.$typpe,$v);
			//源图
			$thumbPath=str_replace($typpe, '_thumb'.$typpe,$v);
			
			$detail[$k]=array(
				'small'=>$smallPath,
				'mid'=>$midPath,
				'big'=>$bigPath,
				'thumb'=>$thumbPath,
				'one'=>$v,
			);
			
		}
//		p($detail);die;
		foreach ($detail as $v) {
			foreach ($v as $value) {
				if(is_file($value)) unlink ($value);
			}
		}
		K('Detail')->where("goods_gid={$gid}")->delete();
		//删除成功提示
		$this->success('删除成功',U('index'),1);
	 }
	
	
	/**
	 * 编辑商品
	 */
	public function edit(){
		//找到对应的gid
		$gid = Q("get.gid","0","intval");
		
		//读取缓存

		$selected = S('selected_' . $gid);
//		p($selected);
		$this->assign('selected',$selected);
		
		if(IS_POST){
//			p($_POST);die;
			if(!$this->model->editData($gid)){
				$this->error($this->model->error);
			}
			S('selected_'.$gid,$_POST['selected']);
			$this->success('修改成功',U('index'));
		}
		
		//获得旧数据
		$oldData = $this->model->where("gid={$gid}")->find();
//		p($oldData);
		$this->assign('oldData',$oldData);
		
		//所属分类
		$cateData = K('Cate')->all();
		$cateData = Data::tree($cateData,'cname');
		$this->assign('cateData',$cateData);
//		p($cateData);die;
		
		//所属品牌
		$brandData = K('Brand')->all();
		$this->assign('brandData',$brandData);
//		p($brandData);die;
		//图片
		$detailData  = K('Detail')->where("goods_gid={$gid}")->find();

		$detailData['shop_pic'] = unserialize($detailData['shop_pic']);
//				p($detailData);
		$this->assign("detailData",$detailData);
		
//		p($brandData);
//		$this->assign('brandData',$brandData);
		
//		p($detailData);die;
		$this->display();
	}
	
	
	
	/**
	 * 删除图片
	 */
	public function delImg(){
		 if(IS_AJAX){
	    	//删除图片
	    	$path = Q('post.path');
	    	//获得类型
			$type = strrchr($path, '.');
			//小图
			$smallPath = str_replace($type, '_small' . $type, $path);
			if(is_file($smallPath)) unlink($smallPath);
			//中图
			$midPath = str_replace($type, '_mid' . $type, $path);
			if(is_file($midPath)) unlink($midPath);
			//大图
			$bigPath = str_replace($type, '_big' . $type, $path);	
			if(is_file($bigPath)) unlink($bigPath);
			//上传图
			$thumbPath = str_replace($type, '_thumb' . $type, $path);	
			if(is_file($thumbPath)) unlink($thumbPath);	
	    				
			if(is_file($path)) unlink($path);
			//删除数据库中对应文章的图片路径
			if($gid =Q('post.gid',0,'intval')){
				$this->model->where("gid={$gid}")->update(array('img'=>''));
			}
	    }
	}
	/**
	 * 删除列表图片
	 */
	public function delPic(){
	    if(IS_AJAX){
	    	//删除图片
	    	$path = Q('post.path');
	    	//获得类型
			$type = strrchr($path, '.');
			//小图
			$smallPath = str_replace($type, '_small' . $type, $path);
			if(is_file($smallPath)) unlink($smallPath);
			//中图
			$midPath = str_replace($type, '_mid' . $type, $path);
			if(is_file($midPath)) unlink($midPath);
			//大图
			$bigPath = str_replace($type, '_big' . $type, $path);	
			if(is_file($bigPath)) unlink($bigPath);
			//上传图
			$thumbPath = str_replace($type, '_thumb' . $type, $path);	
			if(is_file($thumbPath)) unlink($thumbPath);	
	    	//删除本图片			
			if(is_file($path)) unlink($path);
	    }
	}
	
}






 ?>